<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 云淡风轻(97139915@qq.com)
// +----------------------------------------------------------------------

// 留言评论
class MessageAction extends CommonAction{
	public function typeUcMessage()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'UcMessage';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}
	public function typeSupplier()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['rec_module'] = 'Suppliers';		
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}	
	public function typeMessage()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Message';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		//$map['city_id'] = array("in",$_SESSION['admin_city_ids']);
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}
	
	public function typeOrder()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Order';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		$sql = "select m.* from ".C("DB_PREFIX")."message as m left join ".C("DB_PREFIX")."order as o on m.rec_id=o.id left join ".C("DB_PREFIX")."order_goods as og on og.order_id = o.id left join ".C("DB_PREFIX")."goods as g on g.id = og.rec_id where m.rec_module = 'Order' and m.reply_type = 0 and g.city_id in (".implode(",",$_SESSION['admin_city_ids']).")";
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			 if(!$_SESSION['all_city'])       
			 $this->_Sql_list ( $model, $sql );
			 else
			 $this->_list($model,$map);
		}
		$this->display();
		return;
	}
	
	public function typeOrderReConsignment()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'OrderReConsignment';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		$sql = "select m.* from ".C("DB_PREFIX")."message as m left join ".C("DB_PREFIX")."order as o on m.rec_id=o.id left join ".C("DB_PREFIX")."order_goods as og on og.order_id = o.id left join ".C("DB_PREFIX")."goods as g on g.id = og.rec_id where m.rec_module = 'OrderReConsignment' and m.reply_type = 0 and g.city_id in (".implode(",",$_SESSION['admin_city_ids']).")";
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			 if(!$_SESSION['all_city'])       
			 $this->_Sql_list ( $model, $sql );
			 else
			 $this->_list($model,$map);
		}
		$this->display();
		return;
	}
	
	public function typeOrderUncharge()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'OrderUncharge';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		$sql = "select m.* from ".C("DB_PREFIX")."message as m left join ".C("DB_PREFIX")."order as o on m.rec_id=o.id left join ".C("DB_PREFIX")."order_goods as og on og.order_id = o.id left join ".C("DB_PREFIX")."goods as g on g.id = og.rec_id where m.rec_module = 'OrderUncharge' and m.reply_type = 0 and g.city_id in (".implode(",",$_SESSION['admin_city_ids']).")";
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			 if(!$_SESSION['all_city'])       
			 $this->_Sql_list ( $model, $sql );
			 else
			 $this->_list($model,$map);
		}
		$this->display();
		return;
	}

	public function typeSeller()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Seller';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}
	public function typeFeedback()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Feedback';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}
	
	public function typePayment()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Payment';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		$sql = "select m.* from ".C("DB_PREFIX")."message as m left join ".C("DB_PREFIX")."order as o on m.rec_id=o.id left join ".C("DB_PREFIX")."order_goods as og on og.order_id = o.id left join ".C("DB_PREFIX")."goods as g on g.id = og.rec_id where m.rec_module = 'Payment' and m.reply_type = 0 and g.city_id in (".implode(",",$_SESSION['admin_city_ids']).")";
		
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			 if(!$_SESSION['all_city'])       
			 $this->_Sql_list ( $model, $sql );
			 else
			 $this->_list($model,$map);
		}
		$this->display();
		return;
	}
	
	public function typeGoods()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Goods';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		$sql = "Select m.* from ".C("DB_PREFIX")."message as m left join ".C("DB_PREFIX")."goods as g on g.id = m.rec_id where m.reply_type =0 and m.rec_module ='Goods' and g.city_id in (".implode(",",$_SESSION['admin_city_ids']).")";
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			 if(!$_SESSION['all_city'])       
			 $this->_Sql_list ( $model, $sql );
			 else
			 $this->_list($model,$map);
		}
		$this->display();
		return;
	}
	
	public function typeArticle()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Article';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}
	
	public function typeForum()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['reply_type'] = array('between',array(0,3));
		$map['rec_module'] = 'Forum';
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		
		if(intval($_REQUEST['msg_id'])!=0){
			$vo = $model->getById ( intval($_REQUEST['msg_id']) );
			$this->assign ('vo', $vo );	
			$this->display("forumReply");
		}
		else 
			$this->display();
		return;
	}
	
	//查看与回复
	function editforum() {
		$name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$this->assign("param",array('msg_id'=>$id,'reply_type'=>1));  //输出查看回复列表的参数		
		$vo = $model->getById ( $id );
		$this->assign("param_back",array('msg_id'=>$vo['pid'],'reply_type'=>$vo['pid']>0?1:0));  //输出跳回回复列表页的参数
		$this->assign("rec_param",array("id"=>$vo['rec_id']));  //输出关联数据的参数
		$vo['user_info'] = D("User")->getById($vo['user_id']);
		$rec_module = $vo['rec_module'];  //当前留言类型
		
		$this->assign ('vo', $vo );	
		$this->assign('back_action','type'.$rec_module);	
		
		$this->display ();
	}
	
	
	function updateForum()
	{
		//B('FilterString');
		$id = intval($_REQUEST['id']);
		$name=$this->getActionName();
		$model = D ( $name );
		if (false === $data = $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 更新数据
		//$data['title'] = htmlspecialchars($data['title'],ENT_QUOTES);
		//$data['content'] = htmlspecialchars($data['content'],ENT_QUOTES);
		$list=$model->save ($data);
		if (false !== $list) {
		//成功提示
			$msg = '修改讨论:'.strip_tags($data['title']);
			$this->saveLog(1,$id,$msg);
			$this->success (L('EDIT_SUCCESS'));
		} else {
			//错误提示
			$msg = '修改讨论:'.strip_tags($data['title']);
			$this->saveLog(0,$id,$msg);
			$this->error (L('EDIT_FAILED'));
		}
	}
	
	//查看与回复
	function edit() {
		$name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$this->assign("param",array('msg_id'=>$id,'reply_type'=>1));  //输出查看回复列表的参数		
		$vo = $model->getById ( $id );
		$this->assign("param_back",array('msg_id'=>$vo['pid'],'reply_type'=>$vo['pid']>0?1:0));  //输出跳回回复列表页的参数
		$this->assign("rec_param",array("id"=>$vo['rec_id']));  //输出关联数据的参数
		$vo['user_info'] = D("User")->getById($vo['user_id']);
		$rec_module = $vo['rec_module'];  //当前留言类型
		//赋值管理员的回复
		$rep = D("Message")->where("pid=".$vo['id']." and reply_type=2")->find();
		$vo['adm_title'] = $rep['title'];
		$vo['adm_content'] =  $rep['content'];
		$this->assign ('vo', $vo );	
		$this->assign('back_action','type'.$rec_module);	
		
		$this->display ();
	}
	
	public function swTopStatus() {
			$name=$this->getActionName();
			$status = $_REQUEST['status'];
			$model = D ($name);
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			$item = $model->getById($id);
			if($status)
			{
				$list = $model->where($pk."=".$id)->setField('is_top',0);
			}
			else 
			{
				$list = $model->where($pk."=".$id)->setField('is_top',1);
			}
			if ($list!==false) {	
				if($status)
				{		
					$msg = '取消置顶留言:'.$item['title'];
				}
				else
				{
					$msg = '置顶留言:'.$item['title'];	
				}
				$this->saveLog(1,$item['id'],$msg);
				$this->assign ( "jumpUrl", $this->getReturnUrl () );
				$this->success ( L('FORBID_SUCCESS') );
			} else {
				$msg = '置顶留言:'.$item['title'];
				$this->saveLog(0,$item['id'],$msg);
				$this->error  (  L('FORBID_FAILED') );
			}
		}
			
	function update() {
		//B('FilterString');
		$id = intval($_REQUEST['id']);
		$name=$this->getActionName();
		$model = D ( $name );
		if (false === $data = $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 更新数据
		//$data['title'] = htmlspecialchars($data['title'],ENT_QUOTES);
		//$data['content'] = htmlspecialchars($data['content'],ENT_QUOTES);
		$list=$model->save ($data);
		
		if (false !== $list) {
			$msgData = D("Message")->getById($id);  //当前留言的信息
			//开始更新管理员的相应回复
			$adm_reply = D("Message")->where("pid=".$id." and reply_type=2")->find();
			if($adm_reply)
			{
				//已有回复的修改
				if($_REQUEST['adm_title']==""&&$_REQUEST['adm_content']==""||$_REQUEST['adm_title']==""&&$_REQUEST['adm_content']=="<br />")
				{
					//删除管理员回复
					D("Message")->where("pid=".$id." and reply_type=2")->delete();					
				}
				else 
				{
					//$adm_reply['title'] = $_REQUEST['adm_title'];
					$adm_reply['content'] = $_REQUEST['adm_content'];
					$adm_reply['update_time'] = gmtTime();
					$adm_reply['score'] = 0;
					D("Message")->save($adm_reply);  //更新回复
				}
			}
			else 
			{
				//新增管理员回复
				if($_REQUEST['adm_title']!="" || $_REQUEST['adm_content']!="")
				{
					if($_REQUEST['adm_title'] == "")
						$_REQUEST['adm_title'] = $_REQUEST['adm_content'];
					
					$adm_reply['title'] = $_REQUEST['adm_title'];
					$adm_reply['content'] = $_REQUEST['adm_content'];
					$adm_reply['create_time'] = gmtTime();
					$adm_reply['update_time'] = gmtTime();
					$adm_reply['pid'] = $id;
					$adm_reply['reply_type'] = 2;
					$adm_reply['status'] = 1;
					$adm_reply['user_id'] = 0;
					$adm_reply['user_name'] = $_SESSION['adm_name'];
					$adm_reply['rec_module'] = $msgData['rec_module'];
					if ($adm_reply['rec_module'] == 'Order'){
						$adm_reply['rec_id'] = $msgData['rec_id'];
					}else{
						$adm_reply['rec_id'] = $id;
					}
					
					$adm_reply['score'] = 0;
					
					D("Message")->add($adm_reply);
				}
			}
			D("Message")->where("id=".$id)->setField("update_time",gmtTime());  //更新时间
			//成功提示
			$msg = '修改留言:'.$msgData['title'];
			$this->saveLog(1,$msgData['id'],$msg);
			$this->success (L('EDIT_SUCCESS'));
		} else {
			//错误提示
			$msg = '修改留言:'.$msgData['title'];
			$this->saveLog(0,$msgData['id'],$msg);
			$this->error (L('EDIT_FAILED'));
		}
	}

	//禁用与恢复操作
	public function forbid() {
		$name=$this->getActionName();
		$model = D ($name);
		$pk = $model->getPk ();
		$id = $_REQUEST [$pk];
		$msgData = M("Message")->getById($id);
		$condition = array ($pk => array ('in', $id ) );
		$list=$model->forbid ( $condition );
		if ($list!==false) {
			$msg = '禁用留言:'.$msgData['title'];
			$this->saveLog(1,$msgData['id'],$msg);
			$this->success ( L('FORBID_SUCCESS') );
		} else {
			$msg = '禁用留言:'.$msgData['title'];
			$this->saveLog(0,$msgData['id'],$msg);
			$this->error  (  L('FORBID_FAILED') );
		}
	}
	function resume() {
		//恢复指定记录
		$name=$this->getActionName();
		$model = D ($name);
		$pk = $model->getPk ();
		$id = $_GET [$pk];
		$msgData = M("Message")->getById($id);
		$condition = array ($pk => array ('in', $id ) );
		if (false !== $model->resume ( $condition )) {
			$msg = '恢复留言:'.$msgData['title'];
			$this->saveLog(1,$msgData['id'],$msg);
			$this->success ( L('RESUME_SUCCESS') );
		} else {
			$msg = '恢复留言:'.$msgData['title'];
			$this->saveLog(0,$msgData['id'],$msg);
			$this->error ( L('RESUME_FAILED') );
		}
	}
	
	
	public function foreverdelete() {
		//删除指定记录
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			$ids = explode ( ',', $id );
			$names = '';
			$user_ids = '';
			foreach($ids as $idd)
			{
				$names .= M("Message")->where("id=".$idd)->getField("title").",";
				$user_ids .= M("Message")->where(" rec_module='Goods' and id=".$idd)->getField("user_id").",";
			}
			if($names!='')
			{
				$names = substr($names,0,strlen($names)-1);
			}
			if (isset ( $id )) {
				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
				if (false !== $model->where ( $condition )->delete ()) {
					if(floatval(fanweC("MESSAGE_SCORE"))!=0)
					{
						$su = explode ( ',', substr($user_ids,0,strlen($user_ids)-1) );
						if(intval(fanweC('MESSAGE_SCORE_CLS'))==0)
						{
							foreach($su as $uv)
							{
								M("User")->setDec('score','id='.$uv,intval(fanweC('MESSAGE_SCORE')));
								$slog['create_time'] = gmtTime();
								$slog['score'] = intval("-".fanweC('MESSAGE_SCORE'));
								$slog['memo_'.DEFAULT_LANG_ID] = "删除买家评论";
								$slog['user_id'] = $uv;
								$slog['rec_id'] = $uv;
								$slog['rec_module'] = "Goods";
								D("UserScoreLog")->add($slog);
							}
						}
						else
						{
							foreach($su as $uv)
							{
								M("User")->setDec('money','id='.$uv,intval(fanweC('MESSAGE_SCORE')));
								$slog['create_time'] = gmtTime();
								$slog['Money'] = intval("-".fanweC('MESSAGE_SCORE'));
								$slog['memo_'.DEFAULT_LANG_ID] = "删除买家评论";
								$slog['user_id'] = $uv;
								$slog['rec_id'] = $uv;
								$slog['rec_module'] = "Goods";
								D("UserMoneyLog")->add($slog);
							}
						}
					}
					
					//echo $model->getlastsql();
					$model->where(array ("pid" => array ('in', explode ( ',', $id ) ) ))->delete();  //删除相关的回复数据
					$msg = '删除留言:'.$names;
					$this->saveLog(1,0,$msg);
					$this->success (L('DEL_SUCCESS'));
				} else {
					$msg = '删除留言:'.$names;
					$this->saveLog(0,0,$msg);
					$this->error (L('DEL_FAILED'));
				}
			} else {
				$msg = '删除留言:'.$names;
				$this->saveLog(0,0,$msg);
				$this->error ( L('INVALID_OP') );
			}
		}
		$this->forward ();
	}
	
	public function getOrderSN($orderID)
	{
		return D("Order")->where("id = $orderID")->getField('sn');
	}
}
?>